package com.funtl.framework.common;

/**
 * 通用对象工具
 * @author lwm
 *
 */
public class CommonBean {
	/**
	 * 整合原对象数据至新对象中，仅替换空数据 <br />
	 * 如：newAdmin的name属性为空，则将oldAdmin的name属性替换进新对象中
	 * @param newObj 新数据对象
	 * @param oldObj 原数据对象
	 */
	public static void beanToBean(Object newObj, Object oldObj) {
		String[] newPts = CommonReflex.getPropertyTypes(newObj);
		String[] oldPts = CommonReflex.getPropertyTypes(oldObj);
		
		for (int i = 0 ; i < newPts.length ; i++) {
			for (int x = 0 ; x < oldPts.length ; x++) {
				if (newPts[i].equals(oldPts[x])) {
					if (CommonReflex.getValue(newObj, newPts[i]) == null) {
						CommonReflex.setValue(newObj, newPts[i], CommonReflex.getValue(oldObj, oldPts[x]));
						break;
					}
					
					if (CommonReflex.getPropertyType(newObj, newPts[i]).equals(CommonType.JAVA_DOUBLE) && Double.parseDouble(String.valueOf(CommonReflex.getValue(newObj, newPts[i]))) == 0) {
						CommonReflex.setValue(newObj, newPts[i], CommonReflex.getValue(oldObj, oldPts[x]));
						break;
					}
					
					if (CommonReflex.getPropertyType(newObj, newPts[i]).equals(CommonType.JAVA_INTEGER) && Integer.parseInt(String.valueOf(CommonReflex.getValue(newObj, newPts[i]))) == 0) {
						CommonReflex.setValue(newObj, newPts[i], CommonReflex.getValue(oldObj, oldPts[x]));
						break;
					}
					
					if (CommonReflex.getPropertyType(newObj, newPts[i]).equals(CommonType.JAVA_LONG) && Long.parseLong(String.valueOf(CommonReflex.getValue(newObj, newPts[i]))) == 0) {
						CommonReflex.setValue(newObj, newPts[i], CommonReflex.getValue(oldObj, oldPts[x]));
						break;
					}
					
					if (CommonReflex.getPropertyType(newObj, newPts[i]).equals(CommonType.JAVA_SHORT) && Short.parseShort(String.valueOf(CommonReflex.getValue(newObj, newPts[i]))) == 0) {
						CommonReflex.setValue(newObj, newPts[i], CommonReflex.getValue(oldObj, oldPts[x]));
						break;
					}
					
					if (CommonReflex.getPropertyType(newObj, newPts[i]).equals(CommonType.JAVA_BOOLEAN) && String.valueOf(CommonReflex.getValue(newObj, newPts[i])) == null) {
						CommonReflex.setValue(newObj, newPts[i], CommonReflex.getValue(oldObj, oldPts[x]));
						break;
					}
				}
			}
		}
	}
}
